/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import blueprint4j.Constants;
import blueprint4j.db.*;
import blueprint4j.utils.*;


import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;

public class JImageEditor extends JDialog implements ActionListener {
	private FieldImageLarge image_field;
	private boolean editable = true;
	private JButton ok = new JButton("OK");
	private JButton cancel = new JButton("Cancel");
	private JButton left = new JButton("Rotate Left");
	private JButton right = new JButton("Rotate Right");
	private JButton v_flip = new JButton("Flip Vertically");
	private JButton h_flip = new JButton("Flip Horizontally");
	private JPanel main;
	private JImage image;

	public JImageEditor(FieldImageLarge image_field,boolean editable) {
		super();
		this.image_field=image_field;
		this.editable = editable;
		build();
	}
	
	public JImageEditor(FieldImageLarge image_field) {
		this(image_field,true);
	}

	private void build() {
		main=new JPanel(new BorderLayout());

		JPanel ok_cancel=new JPanel(new BorderLayout());
		ok_cancel.add(ok,BorderLayout.WEST);
		ok_cancel.add(cancel,BorderLayout.EAST);

		JPanel edits=new JPanel(new GridLayout(4,1));
		edits.add(left);
		edits.add(right);
		edits.add(v_flip);
		edits.add(h_flip);

		JPanel buttons=new JPanel(new BorderLayout());
		buttons.add(edits,BorderLayout.NORTH);

		ok.addActionListener(this);
		cancel.addActionListener(this);
		left.addActionListener(this);
		right.addActionListener(this);
		v_flip.addActionListener(this);
		h_flip.addActionListener(this);

		if (image_field.get() != null) { 
			image=new JImage(image_field.get().getImage());
		}

		JPanel image_panel=new JPanel(new BorderLayout());
		image_panel.add(image,BorderLayout.CENTER);
		image_panel.setBorder(BorderFactory.createLoweredBevelBorder());

		main.add(Box.createHorizontalStrut(5),BorderLayout.WEST);
		main.add(Box.createVerticalStrut(5),BorderLayout.NORTH);
		main.add(ok_cancel,BorderLayout.SOUTH);
		main.add(image_panel);
		if (editable) {
			main.add(buttons,BorderLayout.EAST);
		}

		getContentPane().add(main);
		setModal(true);
		setTitle("Edit Image");

		pack();
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == ok) {
			try {
				image_field.set(new ImageIcon(image.getImage()));
			} catch (BindException be) {
				Log.debug.out(be);
			}
			setVisible(false);
		} else if (e.getSource() == cancel) {
			setVisible(false);
		} else if (e.getSource() == left) {
			image.setImage(Gui.rotateLeft(image.getImage()));
			pack();
		} else if (e.getSource() == right) {
			image.setImage(Gui.rotateRight(image.getImage()));
			pack();
		} else if (e.getSource() == v_flip) {
			image.setImage(Gui.flipVertically(image.getImage()));
			pack();
		} else if (e.getSource() == h_flip) {
			image.setImage(Gui.flipHorizontally(image.getImage()));
			pack();
		}
	}
}

